package com.bfly.mall.wechat.controller;

import com.bfly.common.ResponseUtil;
import com.bfly.common.page.Pager;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.context.PagerThreadLocal;
import com.bfly.core.security.ActionModel;
import com.bfly.core.security.PermissionCheck;
import com.bfly.mall.wechat.dto.WeChatMpFormDTO;
import com.bfly.mall.wechat.dto.WeChatMpListDTO;
import com.bfly.mall.wechat.entity.WeChatMp;
import com.bfly.mall.wechat.service.IWeChatMpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 微信公众号Controller
 *
 * @author 长沙商脉科技有限公司
 * @date 2021/1/4 14:36
 */
@RestController
@RequestMapping(value = {"/manage/wechat/mp"})
public class WeChatMpController extends BaseController {

    @Autowired
    private IWeChatMpService weChatMpService;

    /**
     * 微信公众号列表
     *
     * @author 长沙商脉科技有限公司
     * @date 2021/1/4 14:38
     */
    @GetMapping(value = "/list")
    public void mpList(HttpServletResponse response) {
        PagerThreadLocal.set(getRequest());
        Pager<WeChatMp> pager = weChatMpService.getPage(null);
        Pager<WeChatMpListDTO> dtoPager = new Pager<>(pager.getPageNo(), pager.getPageSize(), pager.getTotalCount());

        if (pager.getData() != null) {
            List<WeChatMpListDTO> list = new ArrayList<>();
            for (WeChatMp mp : pager.getData()) {
                WeChatMpListDTO dto = new WeChatMpListDTO();
                BeanUtils.copyProperties(mp, dto);
                list.add(dto);
            }
            dtoPager.setData(list);
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(dtoPager));
    }

    /**
     * 所有公众号集合
     *
     * @author 长沙商脉科技有限公司
     * @date 2021/1/5 22:15
     */
    @GetMapping(value = "/all")
    @PermissionCheck(required = false)
    public void mpAll(HttpServletResponse response) {
        List<WeChatMp> mps = weChatMpService.getList();
        List<WeChatMpListDTO> list = new ArrayList<>();
        if (mps != null) {
            for (WeChatMp mp : mps) {
                WeChatMpListDTO dto = new WeChatMpListDTO();
                BeanUtils.copyProperties(mp, dto);
                list.add(dto);
            }
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(list));
    }

    /**
     * 新增微信公众号
     *
     * @author 长沙商脉科技有限公司
     * @date 2021/1/4 14:59
     */
    @PostMapping(value = "/add")
    @ActionModel("编辑微信公众号")
    public void saveWeChatMp(HttpServletResponse response, @Valid @RequestBody WeChatMpFormDTO formDTO, BindingResult result) {
        validData(result);
        weChatMpService.save(formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 编辑微信公众号
     *
     * @author 长沙商脉科技有限公司
     * @date 2021/1/4 15:05
     */
    @PostMapping(value = "/edit")
    @ActionModel("编辑微信公众号")
    public void editWeChatMp(HttpServletResponse response, @Valid @RequestBody WeChatMpFormDTO formDTO, BindingResult result) {
        validData(result);
        weChatMpService.edit(formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 删除微信公众号
     *
     * @author 长沙商脉科技有限公司
     * @date 2021/1/4 15:26
     */
    @PostMapping(value = "/delete")
    @ActionModel(value = "删除微信公众号")
    public void removeWeChatMp(HttpServletResponse response, @RequestBody Integer... Ids) {
        weChatMpService.remove(Ids);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }
}